﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using System.Data.Linq;
namespace BAL
{
    public class Book
    {
        private static string CacheName = "AllBook";
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.Books.ToList(), 0, 60);
            }
        }

        /// <summary>
        /// 检查缓存状态，并读取缓存内容
        /// </summary>
        /// <returns>读取缓存中的内容</returns>
        public static IList<Models.Book> CheckCache()
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            return (IList<Models.Book>)CacheHelper.GetCache(CacheName);
        }

        internal static BookInfo TransBook2BookInfo(Models.Book book)
        {
            BookInfo bookInfo = new BookInfo();

            Utility.SetObjectValueWithTheSamePropertyName(bookInfo, book);

            return bookInfo;
        }

        public static BookInfo AddBook(BookInfo bookInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                Models.Book book = new Models.Book();
                Utility.SetObjectValueWithTheSamePropertyName(book, bookInfo);
                DatabaseHelper.Insert<Models.Book>(book);
                ToCache();
                return TransBook2BookInfo(book);
            }
        }

        public static void EditBook(BookInfo bookInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                try
                {
                    Models.Book book = db.Books.Single(b => b.Id == bookInfo.Id);

                    Utility.SetObjectValueWithTheSamePropertyName(book, bookInfo);

                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                    ToCache();
                }
                catch (ChangeConflictException)
                {
                    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
                    {
                        occ.Resolve(RefreshMode.KeepCurrentValues);
                    }
                }
            }
        }
        public void Update(int id, string name)
        {

        }
        public void Delete(int id)
        {

        }
        public static void DelBook(int id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                DelBook(new int[] { id });
            }
        }

        public static void DelBook(int[] lstId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (lstId.Length > 0)
                {
                    foreach (int i in lstId)
                    {
                        //Knowledge.DelKnowledgeByBook(i);
                        Question.DelQuestionByBook(i);
                    }
                    string sql = string.Format("delete from Book where id in ({0})", string.Join(",", lstId.ConvertToStringArray()));
                    db.ExecuteCommand(sql);
                    ToCache();

                }
            }
        }

        public static void DelBookBySubject(int SubjectID)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
               int[] ids=db.Books.Where(t=> t.Subject==SubjectID).Select(t=>t.Id).ToArray();
               DelBook(ids);
               ToCache();
            }
        }

        public static BookInfo GetBookInfoById(int Id)
        {
            IList<Models.Book> Book_Info = CheckCache();
            return TransBook2BookInfo(Book_Info.Single(t => t.Id == Id));
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    Models.Book book = db.Books.Single(b => b.Id == Id);
            //    return TransBook2BookInfo(book);
            //}
        }
        public static IList<BookInfo> GetBookInfos(int first, int max)
        {
            IList<Models.Book> Book_Info = CheckCache();
            if (max == 0)
            {
                return Book_Info.Select(c => TransBook2BookInfo(c)).ToList();
            }
            else
            {
                return Book_Info.Skip(first).Take(max).Select(c => TransBook2BookInfo(c)).ToList();
            }
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    if (max == 0)
            //    {
            //        return db.GetTable<Models.Book>().Select(c => TransBook2BookInfo(c)).ToList();
            //    }
            //    else
            //    {
            //        return db.GetTable<Models.Book>().Skip(first).Take(max).Select(c => TransBook2BookInfo(c)).ToList();
            //    }
            //}
        }

        public static IList<BookInfo> GetBookInfosBySubject(int SubjectID,int first,int max)
        {
            IList<Models.Book> Book_Info = CheckCache();
            if (max == 0)
            {
                return Book_Info.Where(c => c.Subject == SubjectID).OrderBy(p=>p.Name).Select(c => TransBook2BookInfo(c)).ToList();
            }
            else
            {
                return Book_Info.Where(c => c.Subject == SubjectID).OrderBy(p => p.Name).Skip(first).Take(max).Select(c => TransBook2BookInfo(c)).ToList();
            }
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    if (max == 0)
            //    {
            //        return db.GetTable<Models.Book>().Where(c => c.Subject == SubjectID).Select(c => TransBook2BookInfo(c)).ToList();
            //    }
            //    else
            //    {
            //        return db.GetTable<Models.Book>().Where(c => c.Subject == SubjectID).Skip(first).Take(max).Select(c => TransBook2BookInfo(c)).ToList();
            //    }
            //}
        }

        public static IList<BookInfo> GetBookInfosBySubject(int SubjectID)
        {
            IList<Models.Book> Book_Info = CheckCache();
            return Book_Info.Where(c => c.Subject == SubjectID).OrderBy(p => p.Name).Select(c => TransBook2BookInfo(c)).ToList();
        }


        public static int GetBookInfoCount()
        {
            IList<Models.Book> Book_Info = CheckCache();
            return Book_Info.Count();
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.GetTable<Models.Book>().Count();
            //}
        }

        public static bool CheckBookExistById(int Id)
        {
            return CheckBookExistById(Id, 0);
        }

        public static bool CheckBookExistById(int Id, int SubjectID)
        {
            IList<Models.Book> Book_Info = CheckCache();
            if (SubjectID == 0)
            {
                return Book_Info.Where(w => w.Id == Id).Count() > 0;
            }
            else
            {
                return Book_Info.Where(w => w.Id == Id && w.Subject == SubjectID).Count() > 0;
            }
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    if (SubjectID == 0)
            //    {
            //        return db.Books.Where(w => w.Id == Id).Count() > 0;
            //    }
            //    else
            //    {
            //        return db.Books.Where(w => w.Id == Id && w.Subject == SubjectID).Count() > 0;
            //    }
            //}
        }

        public static bool CheckBookExistByKeyword(string keyword, int editid)
        {
            IList<Models.Book> Book_Info = CheckCache();
            return Book_Info.Where(t => t.Keyword == keyword && t.Id != editid).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.Books.Where(t => t.Keyword == keyword && t.Id != editid).Count() > 0;
            //}
        }

        public static bool CheckBookExistByName(string name)
        {
            return CheckBookExistByName(name,"", 0);
        }

        public static bool CheckBookExistByName(string name,string keyword, int SubjectID)
        {
            IList<Models.Book> Book_Info = CheckCache();
            if (SubjectID == 0)
            {
                return Book_Info.Where(w => w.Name == name).Count() > 0;
            }
            else
            {
                return Book_Info.Where(w => w.Keyword == keyword || (w.Subject == SubjectID && w.Name == name)).Count() > 0;
            }
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    if (SubjectID == 0)
            //    {
            //        return db.Books.Where(w => w.Name == name).Count() > 0;
            //    }
            //    else
            //    {
            //        return db.Books.Where(w => w.Keyword == keyword || (w.Subject == SubjectID && w.Name == name)).Count() > 0;
            //    }
            //}
        }
    }
}
